package com.itsm.sheet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsm.common.sheet.JzsyEventSheetDto;
import com.itsm.common.sheet.TransferSheetDto;
import com.itsm.sheet.entity.JzsyEventSheet;
import com.itsm.sheet.entity.JzsyEventSheetProcess;
import com.itsm.sheet.mapper.JzsyEventSheetMapper;
import com.itsm.sheet.mapper.JzsyEventSheetProcessMapper;
import com.itsm.sheet.service.IJzsyEventSheetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class JzsyEventSheetServiceImpl implements IJzsyEventSheetService {
    @Autowired
    private JzsyEventSheetMapper jzsyEventSheetMapper;


    @Autowired
    private JzsyEventSheetProcessMapper jzsyEventSheetProcessMapper;


    public JzsyEventSheet createSheet(JzsyEventSheetDto jzsyEventSheetDto){
        JzsyEventSheet jzsyEventSheet = new JzsyEventSheet();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BeanUtils.copyProperties(jzsyEventSheetDto,jzsyEventSheet);
        try{
            Date tempTime = sdf.parse(jzsyEventSheetDto.getSubmitTime());
            LocalDateTime submitTime = tempTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            jzsyEventSheet.setSubmitTime(submitTime);
        }catch (Exception e){

        }
        jzsyEventSheetMapper.insert(jzsyEventSheet);
        return jzsyEventSheet;
    }

    public List<JzsyEventSheet> getSheetByUser(Long userId){
        QueryWrapper<JzsyEventSheet> wu = new QueryWrapper<JzsyEventSheet>();
//        wu.select("user_id,user_name ,pwd").eq("phone",phone);
//        List<FcqUser> lu = fcqUserMapper.selectList(wu);
//        if(!lu.isEmpty()){
//            if(lu.size() == 1){
//                return userToVo(lu.get(0));
//            }
//        }
        return jzsyEventSheetMapper.getSheetByUser(userId);

    }


    public JzsyEventSheet closeSheet(TransferSheetDto transferSheetDto){
        JzsyEventSheetProcess jzsyEventSheetProcess = jzsyEventSheetProcessMapper.selectById(transferSheetDto.getSheepProcessId());
        jzsyEventSheetProcess.setProblemReason(transferSheetDto.getProblemReason());
        jzsyEventSheetProcess.setResolvent(transferSheetDto.getResolvent());
        jzsyEventSheetProcess.setAnswer(transferSheetDto.getAnswer());
        jzsyEventSheetProcess.setResolutionTime(transferSheetDto.getResolutionTime());
        jzsyEventSheetProcessMapper.updateById(jzsyEventSheetProcess);

        //修改工单状态
        JzsyEventSheet jzsyEventSheet = jzsyEventSheetMapper.selectById(jzsyEventSheetProcess.getSheetId());
        jzsyEventSheet.setSheetStatus("DOING");
        jzsyEventSheetMapper.updateById(jzsyEventSheet);

        return jzsyEventSheet;

    }


}
